
package com.tools.cleanmaster.ui.view.trash;

import android.graphics.drawable.Drawable;

import com.tools.cleanmaster.R;
import com.tools.cleanmaster.appinfo.AppInfoSnapshot;
import com.tools.cleanmaster.appinfo.AppManager;
import com.tools.cleanmaster.model.TrashType;
import com.tools.cleanmaster.model.item.AppTrashItem;
import com.tools.cleanmaster.model.item.TrashItem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@SuppressWarnings("static-access")
public class TrashCacheGroup extends TrashGroup {

    private final AppManager mAppMgr;
    public static class AppCacheTrashGroup extends TrashGroupWithIndicator {
        private final AppInfoSnapshot mAppInfo;

        AppCacheTrashGroup(String pkgName, TrashGroup group, AppInfoSnapshot appInfo,
                ArrayList<TrashItem> items) {
            super(group);
            mAppInfo = appInfo;
            mIsChecked = mIsCheckedDefault = true;
            for (TrashItem trashItem : items) {
                addChildItem(new TrashViewItemSingle(trashItem, this) {
                    @Override
                    public String getTitle() {
                        return ((AppTrashItem) mTrashItem).itemName;
                    }

                    @Override
                    public Drawable getIcon() {
                        return mContext.getResources().getDrawable(
                                R.drawable.icon_trash_threemenu);
                    }
                });
            }
            // 设置check状态
            setCheckStateByChild();
            Collections.sort(mChildItems);
        }

        @Override
        public String getTitle() {
            return mAppInfo == null ? "" : mAppInfo.getLabel();
        }

        @Override
        public Drawable getIcon() {
            return mAppInfo == null ? null : mAppInfo.getIcon();
        }

        @Override
        public void fillTrashes(Map<TrashType, List<TrashItem>> trashes) {
            // do in constructor
        }
    }

    public TrashCacheGroup() {
        super(null);
        mAppMgr = AppManager.getInstance(mContext);
    }

    @Override
    public Drawable getIcon() {
        return mContext.getResources().getDrawable(R.drawable.icon_trash_clean_cache);
    }

    @Override
    public String getTitle() {
        return mContext.getResources().getString(R.string.cache_trash_item);
    }

    boolean isCleanSuggestMatched(int cleanSuggest) {
        return cleanSuggest == AppTrashItem.CLEAN_SUGGEST_CLEAN;
    }

    @Override
    public void fillTrashes(Map<TrashType, List<TrashItem>> trashes) {
        List<TrashItem> list = trashes.get(TrashType.APP_TRASH_FILE);
        if (list != null && !list.isEmpty()) {
            Map<String, ArrayList<TrashItem>> pkgCacheMap = new HashMap<String, ArrayList<TrashItem>>();
            for (TrashItem trashItem : list) {
                ArrayList<TrashItem> items = null;
                String pkgName = trashItem.pkgName;
                if (!pkgCacheMap.containsKey(pkgName)) {
                    items = new ArrayList<TrashItem>();
                    pkgCacheMap.put(pkgName, items);
                } else {
                    items = pkgCacheMap.get(pkgName);
                }
                items.add(trashItem);
            }
            Set<String> pkgs = pkgCacheMap.keySet();
            for (String pkg : pkgs) {
                AppCacheTrashGroup appCacheTrashGroup = new AppCacheTrashGroup(pkg, this,
                        mAppMgr.getAppInfo(pkg), pkgCacheMap.get(pkg));
                addChildItem(appCacheTrashGroup);
            }
            // 设置check状态
            setCheckStateByChild();
            Collections.sort(mChildItems);
        }
    }

}
